var votes_num=new Array();  //VOTES IN NUMBER - paralel
var votes_prc=new Array();  //VOTES IN PERCENT- paralel
var votes_checkbox = new Array();   //- paralel
var answer_id=new Array();  //- paralel
var answer_text=new Array();//- paralel
jQuery('.lss-view-results').live('click',function(){
    okreni_anketu('result');
});
jQuery('.lss-view-poll').live('click',function(){
    okreni_anketu('poll');        
});
function okreni_anketu(jQuerysection){
    switch(jQuerysection){
        case 'poll':
            jQuery('.lss-answer').replaceWith(answers);
            jQuery('.lss-vote').replaceWith(votesectionbottom);
            break;
        case 'result':  
            jQuery('.lss-answer').replaceWith(jQuery("<div class='lss-answer'><div id='lss-answer"+LS_pollid+"'></div></div>"));
            jQuery('.lss-vote').replaceWith(resultsectionbottom);                           
            kreiraj_rezultate();
            jQuery('.lss-answer-span').width('100%'); //FIX RESULT ANSWER TEXT WIDTH to 100%
            break;
    }
}
jQuery('body').bind('priview_result',function(jQuerysection){        
    okreni_anketu('result');  
})
jQuery('body').bind('priview_poll',function(jQuerysection){        
    okreni_anketu('poll'); 
})
function ucitaj_rezultate(){
    //console.log('ucitaj_rezultate') 
    jQuery.ajax({
        type: 'GET',
        url: sys_url+'onsite_anketa/citaj_rezultate/?jsoncall=?',
        data: ({ poll_id : LS_pollid }),
        dataType: 'jsonp',
        success: function(data){
            var counter = 0;
            jQuery.each(LS_answers, function(index, answer) { 
                jQuery.each(answer, function(key, value) {
                    if(key=='id'){
                        votes_num[index] = votes_count(value, data);
                        answer_id[index] = value;
                    }
                    if(key=='text'){
                        answer_text[index] = value;                            
                    }
                });
            });
            if(LS_pollownanswer==1){
                counter = 0;
                jQuery.each(data, function(index) {
                    if(data[index].answer_id==0){
                        counter ++;    
                    }
                });
                var posistion = votes_num.length
                votes_num[posistion] = counter;
                answer_id[posistion] = 0;
                answer_text[posistion] = 'Ostalo';
            }
            /*COUNT TOTAL VOTES AND RESULTS IN PERCENTAGE VALUE --> FORMULA X:100=CUR:TOTAL*/
            izracunaj_procente_glasanja();
        },
        error:function(content){alert("Error: " + content);}
    });
}
function votes_count(jQueryanswer_id, data){
    //console.log('votes_count') 
    var counter = 0;
    jQuery.each(data, function(index, answer) { 
        jQuery.each(answer, function(key, value) {
            if((key=='answer_id')&&(value==jQueryanswer_id)){
                counter++;
            }
        })
    });
    return counter;
}
/*COUNT RESULTS IN PERCENTAGE VALUE --> FORMULA X:100=CUR:TOTAL*/    
function izracunaj_procente_glasanja(){ 
    //console.log('percent_total_votes')   
    LS_totalvotes = 0;
    votes_prc = new Array();
    jQuery.each(votes_num, function(index, value) {
        LS_totalvotes +=value;                                              
    });     
    jQuery.each(votes_num, function(index, value) {                          
        if (value>0){
            cur= (value*100)/LS_totalvotes;    
        }else{
            cur=0;
        }
        votes_prc[index]=Math.round(cur);                    
    });
}
var bg_image;
var bg_color;
function set_loader(){
    bg_image = jQuery('#lss-vote-button').css('background-image') ;
    bg_color = jQuery('#lss-vote-button').css('background-color') ;
    jQuery('#lss-vote-button span').css('visibility','hidden');
    jQuery('#lss-vote-button').css('background', 'url('+sys_url+'assets/img/backgrounds/vote-loader-eeeeee.gif) no-repeat center center '+bg_color);
}
function clear_loader(){
    jQuery('#lss-vote-button').css('background', bg_image+' repeat-x '+bg_color);
    jQuery('#lss-vote-button span').css('visibility','visible');                     
}
jQuery('#lss-vote-button').live('click',function(){
    set_loader();
    if(LS_pollstatus==2){
        alert('Anketa je zatvorena za glasanje.');
        clear_loader()
        return;
    }
    if(LS_repeatvoters==2){
        if(get_cookie('voted')=='true'){
            alert('Već ste glasali. Hvala.');
            clear_loader()
            return;
        } 
    }
    if(LS_pollownanswer) own_answer_text = jQuery('#LSI_OtherText').val();
    switch (LS_pollmultipleselection){
        case '0' :
            glasaj__opcija_radio();
            break;
        case '1' :
            glasaj__opcija_checkbox();
            break;
    }
})
function glasaj__opcija_radio(){
    answerid = jQuery("input[name='LSI_answer"+LS_pollid+"']:checked").val();
    if (answerid!=undefined) {
        if((LS_pollownanswer==1)&&(answerid==0)){
            if(jQuery('#LSI_OtherText').val()==""){
                alert('Molimo unesite odgovor');
                clear_loader()
                return false;
            }
        }
        jQuery.ajax({
            type: 'GET',
            url: sys_url+'onsite_anketa/glasaj__opcija_radio/?jsoncall=?',
            data: ({ answerid : answerid, poll_id : LS_pollid, own_anser : LS_pollownanswer, own_answer_text:own_answer_text}),
            dataType: 'jsonp',
            success: function(data){
                if(data.result == true){
                    jQuery.each(answer_id, function(index) {
                        if(answer_id[index]==answerid){
                            votes_num[index]+=1;
                        }                 
                    });
                    izracunaj_procente_glasanja();
                    clear_loader();
                    okreni_anketu('result');
                    if(LS_repeatvoters==2) document.cookie = 'voted=true'; 
                }else{
                    alert(data.reason);
                    clear_loader();
                }

            },
            error:function(content){alert("Error: " + content);}
        });
    }else{
        alert('Molimo izaberite odgovor!')
        clear_loader();
    }
}
function glasaj__opcija_checkbox(){
    var counter = 0;        
    votes_checkbox = new Array();
    jQuery("input[name='LSI_answer"+LS_pollid+"']").each( function(index) {
        if(this.checked){                
            votes_checkbox[counter] = this.value;
            counter++; 
        }
    });
    if(counter>0){
        answerid = jQuery('#OtherAnswer').is(':checked');
        if((LS_pollownanswer==1)&&(answerid)){
            if(jQuery('#LSI_OtherText').val()==""){
                alert('Molimo unesite odgovor');
                clear_loader()
                return false;
            }
        }
        jQuery.ajax({
            type: 'GET',
            url: sys_url+'onsite_anketa/glasaj__opcija_checkbox/?jsoncall=?',
            data: ({ answerid : votes_checkbox, poll_id : LS_pollid, own_anser : LS_pollownanswer, own_answer_text:own_answer_text}),
            dataType: 'jsonp',
            success: function(data){
                if(data.result == true){
                    jQuery.each(answer_id, function(index1) {
                        jQuery.each(votes_checkbox, function(index2) {
                            if(answer_id[index1]==votes_checkbox[index2]){
                                votes_num[index1]+=1;
                            }  
                        });               
                    });      
                    izracunaj_procente_glasanja();
                    clear_loader()
                    okreni_anketu('result');
                    if(LS_repeatvoters==2) document.cookie = 'voted=true';
                }else{
                    alert(data.reason);
                    clear_loader();
                }

            },
            error:function(content){alert("Error: " + content);}
        });
    }else{
        alert('Molimo izaberite odgovor!')
        clear_loader();
    }
}
function get_cookie ( cookie_name ){
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results )
        return ( unescape ( results[2] ) );
    else
        return null;
}
//console.timeEnd('anketa_test_1');  